package com.ketai.dao.impl;

import com.ketai.dao.BaseDao;
import com.ketai.dao.ResearchDao;
import com.ketai.vo.ResearchVo;

import javax.naming.NamingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @date 2022-08-10 13:34
 */

public class ResearchDaoImpl extends BaseDao implements ResearchDao {

    @Override
    public List<ResearchVo> getResearchVoList(String sql, Object[] params) throws SQLException, NamingException {
        ResultSet resultSet = query(sql, params);
        List<ResearchVo> researchVos = new ArrayList<>();
        while (resultSet.next()) {
            ResearchVo researchVo = new ResearchVo();
            researchVo.setId(resultSet.getLong("id"));
            researchVo.setTitle(resultSet.getString("title"));
            researchVo.setImgUrl(resultSet.getString("img"));
            researchVo.setDetail(resultSet.getString("detail"));
            researchVos.add(researchVo);
        }
        return researchVos;
    }

    @Override
    public long getTotalResearchCount(String sql, Object[] params) throws SQLException, NamingException {
        ResultSet resultSet = query(sql, params);
        while (resultSet.next()) {
            return resultSet.getLong("total");
        }
        return 0;
    }
}
